﻿<%@ Page Title="" Language="C#" MasterPageFile="~/SIPO.master" AutoEventWireup="false" CodeFile="recExpenselist.aspx.cs" Inherits="SIPO.Web.Lists.Transaction.recExpenselist" %>

<asp:Content ID="Content1" ContentPlaceHolderID="body" Runat="Server">
  <link type="text/css" rel="stylesheet" href="<%= ResolveUrl("~/styles/ui.jqgrid.css") %>" />
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/i18n/grid.locale-id.js") %>"></script>
    <script type="text/javascript" src="<%= ResolveUrl("~/scripts/jquery.jqGrid.min.js") %>"></script>
    <script src="<%= ResolveUrl("~/scripts/helper.js") %>" type="text/javascript"></script>
    <div>
        <button id="btnAdd" type="button" onclick="OpenForm('add');">
            Tambah</button>
        <button id="btnEdit" type="button" onclick="OpenForm('edit');">
            Ubah</button>
        <button id="btnDelete" type="button" onclick="OpenForm('delete');">
            Hapus</button>
            <button id="btnReview" type="button" onclick="OpenForm('review');">
            Setujui</button>
    </div>
     <br />
    <br />
    <table id="table" class="scroll" cellpadding="0" cellspacing="0">
    </table>
    <div id="pager" class="scroll">
    </div>
    <br />
    <div id="dialog1" title="Default dialog">
    </div>
    <script type="text/javascript">

        $("#btnAdd").button({ icons: { primary: "ui-icon-plus"} });
        $("#btnEdit").button({ icons: { primary: "ui-icon-pencil"} });
        $("#btnDelete").button({ icons: { primary: "ui-icon-trash"} });
        $("#btnReview").button({ icons: { primary: "ui-icon-locked"} });
    </script>
     <script type="text/javascript">

        var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Transaction/recExpensehandler.ashx") %>';

         $('#table').jqGrid({
             url: handlerUrl + '?method=getdata',
             datatype: 'json',
             colNames: ['ID','IsActive' ,'Tgl. Transaksi', 'No. Bukti', 'Tujuan Penggunaan', 'Alokasi Penggunaan', 'Tgl. Persetujuan', 'Nilai (Rp)','Nama Lampiran','File Bukti'],
             colModel: [
           		{ name: 'ExpenseID', key: true, hidden: true, index: 'ExpenseID', width: 5 },
                { name: 'IsActive', index: 'IsActive', hidden: true, width: 5 },
           		{ name: 'TransDate', index: 'TransDate', width: 75, align: 'center', searchoptions: { sopt: ['eq', 'cn'] }, formatter: 'date', formatoptions: { newformat: 'd M Y'} },
                { name: 'TransNo', index: 'TransNo', width: 85, align: 'center', searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'TransDesc', index: 'TransDesc', width: 200, align: 'left', searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'TransAlocationName', index: 'TransAlocationName', width: 200, searchoptions: { sopt: ['eq', 'cn'] }, align: 'left' },
                { name: 'ReviewDate', index: 'ReviewDate', width: 75, align: 'center', formatter: 'date', formatoptions: { newformat: 'd M Y' }},
                { name: 'TransValues', index: 'TransValues', width: 85, formatter: 'number', searchtype: 'number', align: 'right' },
                { name: 'AttachFileName', index: 'AttachFileName', width: 75, align: 'left', searchoptions: { sopt: ['eq', 'cn']} },
                { name: 'ExpenseID', index: 'ExpenseID', align: 'center', width: 50, search: false, formatter: returnMyLink },
           	],
             rowNum: 10,
             id: 'ExpenseID',
             multiselect: true,
             rowList: [5, 10, 15, 20],
             pager: '#pager',
             sortname: 'TransDate',
             sortorder: "asc",
             viewrecords: true,
             jsonReader: { repeatitems: false },
             height: '100%',
             width: 1250,
             caption: 'Penggunaan Dana',
             ajaxGridOptions: { cache: false }
         });

         $("#table").jqGrid('navGrid', '#pager', { edit: false, add: false, del: false, searchtext: 'Search', refreshtext: 'Refresh' },
        {}, {}, {}, { closeOnEscape: true, multipleSearch: true, closeAfterSearch: true, showQuery: false });

         function returnMyLink(cellValue, options, rowdata) {
             var handleurlDownload = '<%= ResolveUrl("~/ajaxhandler/loaddata/downloadhandler.ashx?method=downloadExpense") %>';
             //return "<a target='_blank' href='" + handleurlDownload + "&sanction_id=" + cellValue + "' >download</a>";
             return "<a  href='#' onclick='downloadfile(" + cellValue + ")' >download</a>";
         }
         function downloadfile(cellValue) {
             var objrecImposition = new Object();
             objrecImposition.ExpenseID = encodeURIComponent(cellValue);
             jsonData = JSON.stringify(objrecImposition);
             var handlerUrl = '<%= ResolveUrl("~/ajaxhandler/loaddata/downloadhandler.ashx") %>';
             $.ajax({
                 type: 'POST',
                 url: handlerUrl + "?method=checkfileExpense",
                 contentType: 'application/json',
                 dataType: 'json',
                 data: jsonData,
                 success: function (data) {
                     if (data.IsSuccess) {
                         window.open(handlerUrl + '?method=downloadExpense&expenseId=' + cellValue, 'popUpWindow', 'height=400,width=800,left=300,top=200,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no, status=yes');
                         //return "<a target='_blank' href='" + handlerUrl + "method=download&sanction_id=" + cellValue + "' >download</a>";
                     }
                     else {
                         alert(data.Message);
                     }
                 },
                 error: function (response) {
                     //var r = JSON.parse(response.responseText);
                     alert("Error Check File Download"); //Message: " + r.Message);
                 }
             });

         }

         function OpenForm(ctl) {

             var formUrl = '<%= ResolveUrl("~/forms/transaction/recExpenseform.aspx") %>';

             var grid = $('#table');
             var selIds = grid.jqGrid('getGridParam', 'selarrrow'),
                selText = [];
             selreview = [];
             selIsactive = [];
             $.each(selIds, function () {
                 selText.push(grid.jqGrid('getCell', this, 'ExpenseID'));
                 selreview.push(grid.jqGrid('getCell', this, 'ReviewDate'));
                 selIsactive.push(grid.jqGrid('getCell', this, 'IsActive'));
             });
             if (ctl == 'edit') {

                 if (selIds.length == 0)
                     alert('Pilih data yang akan di ubah!');
                 else {
                     if (selIds.length > 1) {
                         alert('Pilih satu record untuk ubah data!');
                     }
                     else if (selIds.length == 1 && selIsactive == 'true' && selreview.toString().toLowerCase().indexOf('2') < 0) {

                         $('#dialog1').load(formUrl + '?id=' + selText).dialog({ autoOpen: false, modal: true, width: 420, title: 'Ubah Penggunaan Dana' });
                         $("#dialog1").dialog('open');
                     }
                     else {
                         alert('data yang anda pilih tidak dapat di ubah!');
                     }
                 }
             }
             else if (ctl == 'add') {
                 $('#dialog1').load(formUrl).dialog({ autoOpen: false, modal: true, width: 420, title: 'Tambah Penggunaan Dana' });
                 $("#dialog1").dialog('open');
             }
             else if (ctl == 'review') {
                 if (selIds.length == 0)
                     alert('Pilih data yang akan di setujui !');
                 else {
                     if (selIds.length > 1) {
                         alert('Pilih satu record untuk setujui data!');
                     }
                     else if (selIds.length == 1 && selIsactive == 'true' && selreview.toString().toLowerCase().indexOf('2') < 0) {
                         $('#dialog1').load(formUrl + '?id=' + selText + '&review=true').dialog({ autoOpen: false, modal: true, width: 440, title: 'Persetujuan Penggunaan Dana' });
                         $("#dialog1").dialog('open');
                     }
                     else {
                         alert('data yang anda pilih tidak dapat di setujui!');
                     }
                 }
             }
             else if (ctl == 'delete') {
                 if (selIds.length == 0)
                     alert('Pilih data yang mau di hapus!');
                 else {
                     if (selreview.toString().toLowerCase().indexOf('2') < 0) {
                         var result = window.confirm('Apakah Anda yakin untuk menghapus data ini?');
                         if (result) {

                             var objrecExpense = new Object();
                             objrecExpense.ExpenseID = encodeURIComponent(selText);

                             jsonData = JSON.stringify(objrecExpense);

                             CallAjax('<%= ResolveUrl("~/AjaxHandler/Transaction/recExpensehandler.ashx") %>', "?method=delete", jsonData);

                             setTimeout("$('#table').trigger('reloadGrid');", 3000);
                         }
                     }
                     else {
                         alert('data yang anda pilih tidak dapat di hapus!');
                     }
                 }
             }
             else {
                 alert('data yang anda pilih tidak dapat di hapus!');
             }
        }

    </script>
</asp:Content>

